<?php

/**
 * This file is part of geekPWS.
 * 
 * Copyright (C) Iain Learmonth 2010.
 *
 * geekPWS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * geekPWS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with geekPWS.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package geekPWS
 * @subpackage CoreBlogSystem
 */

/**
 * This represents a subset of blog posts, that would be useful
 * when generating an index listing of recent blog posts.
 * 
 * @author Iain Learmonth <adapa@hotmail.co.uk>
 * @version 0.1
 */
class BlogIndex
{
	/**
	 * The array containing BlogPost instances
	 * 
	 * @var BlogPost[]
	 */
	private $index;
	
	/**
	 * The number of blog posts in the array
	 * 
	 * @var Integer
	 */
	private $size;
	
	/**
	 * Is there a next page of posts?
	 * 
	 * @var Boolean
	 */
	private $isNext;
	
	/**
	 * Is there a previous page of posts?
	 * 
	 * @var Boolean
	 */
	private $isPrev;
	
	/**
	 * Constructor for BlogIndex which creates an index
	 * of recent blog posts.
	 * 
	 * @param Integer Where to start the index from
	 */
	public function __construct($start)
	{
		$query = "SELECT `id` FROM `blog_post` LIMIT $start,10";
		$result = mysql_query($query);
		$count = 0;
		while ( ($row = mysql_fetch_array($result)) != null )
		{
			$index[0] = new BlogPost($count);
			++$count;
		}
		
		if ( $start > 0 )
		{
			$isPrev = true;
		} else {
			$isPrev = false;
		}
		
		if ( mysql_fetch_array($result) != null )
		{
			$isNext = true;
		} else {
			$isPrev = false;
		}
	}
	
	/**
	 * Is there a next page of results?
	 * 
	 * @return Boolean true if there's a next page
	 */
	public function isNext()
	{
		return $this->isNext;
	}

	/**
	 * Is there a previous page of results?
	 * 
	 * @return Boolean true if there's a previous page
	 */	
	public function isPrev()
	{
		return $this->isPrev;
	}
	
	
	public function getBlogPostFromIndex($index)
	{
		if ( $index >= 0 && $index < $this->size )
		{
			return $index[$index];
		} else {
			die("BlogIndex: Blog Post Out Of Index Range");
		}
	}
}